<?php

/**
 * Created by phpstorem.
 * User: yangakw@qq.com
 * Sign: 猥琐发育，别浪
 * Date: 2018-06-23
 * Time: 上午 10:20
 */
class Msg extends CI_Controller
{
    /**
     * 缓存文章
     */
    public function add_article(
        $openid,
        $url="https://mp.weixin.qq.com/s?__biz=MzA4NzkxNTc4Mg==&mid=2650771541&idx=1&sn=2f7f144fee1d16575286101b366c4d4a&scene=0"
    ){
        $user = $this->load->model('user')->by_openid($openid);
        if(empty($user)){
            exit("用户获取失败");
        }
        $user_id = $user->id;
        $this->load->library('Simple_html_dom.php');
        $html = new simple_html_dom();
        $html->load_file($url);
        $page_content = $html->find('html',0);
        foreach($page_content->find("img") as &$img){
            $img->src = "/i?src=".$img->getAttribute("data-src");
        }
        $html = $page_content->outertext;
        $source_url = $url;
        $code = md5($url);
        $data = [
            "user_id"=>$user_id,
            "code"=>$code,
            "source_url"=>$source_url,
            "html"=>$html,
        ];
        $this->load->model('article')->save($data);
    }

    public function add_ad_pic(
        $openid,
        $pic=''
    ){
        $user = $this->load->model('user')->by_openid($openid);
        if(empty($user)){
            exit("用户获取失败");
        }
        $user_id = $user->id;
        $data = [
            "user_id"=>$user_id,
            "pic"=>$pic,
        ];
        $this->load->model('ad')->save($data);
    }
    public function add_ad_phone(
        $openid,
        $phone=''
    ){
        $user = $this->load->model('user')->by_openid($openid);
        if(empty($user)){
            exit("用户获取失败");
        }
        $user_id = $user->id;
        $data = [
            "user_id"=>$user_id,
            "phone"=>$phone,
        ];
        $ad =  $this->load->model('ad')->by_user_id($user_id);
        if(!empty($ad)){
            $data['id'] = $ad->id;
        }
        $this->load->model('ad')->save($data);
    }

    public function handle(){
        define("TOKEN", "weixin");
        $wechatObj = $this->load->library('WechatCallbackapi');
        $wechatObj = new WechatCallbackapi();
        if (!isset($_GET['echostr'])) {
            $wechatObj->responseMsg();
        }else{
            $wechatObj->valid();
        }
    }

    public static  function re_image($obj){
        $content = '';
        return $content;
    }
    public static function re_text($obj){
        $content = '';
        $msg = $obj->Content;
        switch($msg){
            case '1':
                $content = sprintf("<a href='%s'>%s</a>",SITE_DOMAIN."/article_manage","我的文章");
                break;
            case '2':
                $content = sprintf("<a href='%s'>%s</a>",SITE_DOMAIN."/fee","我的账户");
                break;
            case '3':
                $content = sprintf("<a href='%s'>%s</a>",SITE_DOMAIN."/ad_manage","我的推广");
                break;
            case '4':
                $content = sprintf("<a href='%s'>%s</a>",SITE_DOMAIN."/t","测试");
                break;
            default:
                $content = self::menu();
        }
        return $content;
    }
    static function menu(){
        return sprintf("%s\n%s\n%s\n%s\n%s\n",
            "【回复下列数字执行操作】",
              "【1】我的文章",
                  "【2】我的账户",
                  "【3】我的推广",
                  "【4】测试"
            );
    }

}